<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/21 0021
 * Time: 上午 11:29
 */
namespace app\message\model;
use think\console\command\make\Model;
use think\Db;

class Message extends Model{
    public function say_add($data){
        $add = Db::name('message')->insertGetId($data);
        return $add;
    }
    public function say_update($data){
        $data['updatetime']=date('Y-m-d H:i:s');
        $update = Db::name('message')->where(['id'=>$data['id']])->update($data);
        return $update;
    }
    public function say_list($data){
        $where = 'ms.del=1';
        $where.= isset($data['keyword'])? ' and CONCAT(content) like "%'.$data['keyword'].'%"':'';
        #总条数
        $total = Db::name('message')->alias('ms')
            ->join('member mb','ms.uid=mb.id')
            ->where($where)->count();
        #总页数
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }
        #内容
        $field = 'username,title,content,ms.updatetime,ms.createtime';
        $list = Db::name('message')->alias('ms')
            ->join('member mb','ms.uid=mb.id')
            ->where($where)
            ->page($data['curr'],$data['limits'])
            ->field($field)
            ->order(['ms.updatetime'=>'desc'])
            ->select();

        #结果
        $res = [
            'list'      =>$list,
            'total'     =>$total,
            'page'      =>$page,
            'curr'      =>$data['curr'],
            'limits'    =>$data['limits'],
        ];
        return $res;
    }
    public function say_list_info($data){
        $where = 'del=1';
        $where.=' and uid ='.$data['uid'];
        $where.= isset($data['keyword'])? ' and CONCAT(content) like "%"'.$data['keyword'].'"%"':'';
        #总条数
        $total = Db::name('message')->where($where)->count();
        #总页数
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }
        #内容
        $list = Db::name('message')->where($where)->page($data['curr'],$data['limits'])->select();
        #结果

        $res = [
            'list'      =>$list,
            'total'     =>$total,
            'page'      =>$page,
            'curr'      =>$data['curr'],
            'limits'    =>$data['limits'],
        ];
        return $res;
    }
}